<template>
	<div>
		<!--编辑界面-->
		<el-dialog v-loading.fullscreen.lock="loading" element-loading-text="处理中......" title="编辑条款" :visible.sync="dialogVisible" size="small" :close-on-click-modal="false" :close-on-press-escape="false" :before-close="off" :top="dialog_margin_top">
			  <el-form label-width="160px">
			  <el-row :span="24">
			  	<el-col :span="12">
			  		<el-form-item label="归属险类" required>
	  					<el-select v-model="formData.ownerriskcode" filterable size="small"  @change="newKindCode()" disabled>
							<el-option v-for="item in riskOptions" :key="item.riskcode" :label="item.riskcode + '-' + item.riskcname" :value="item.riskcode"></el-option>
						</el-select>
					</el-form-item>
					<el-form-item label="主险/附加险" required>
						<el-select v-model="formData.kindType" filterable size="small"  @change="newKindCode()" disabled>
							<el-option v-for="item in kindTypeOptions" :key="item.value" :label="item.value + '-' + item.label" :value="item.value"></el-option>
						</el-select>
					</el-form-item>
					<el-form-item label="条款代码" required>
						<el-input v-model="formData.kindcode" size="small" disabled></el-input>
					</el-form-item>
					<el-form-item label="条款中文名称" required>
						<el-input v-model="formData.kindcname" :disabled="disabledGroup.kindcname" :maxlength="255" size="small"></el-input>
					</el-form-item>
					<el-form-item label="条款英文名称" required>
						<el-input v-model="formData.kindename" :disabled="disabledGroup.kindename" :maxlength="255" size="small"></el-input>
					</el-form-item>
					<el-form-item label="条款版本" required>
						<el-select v-model="formData.kindversion" filterable size="small" >
							<el-option v-for="item in versionOptions" :key="item.value" :label="item.value" :value="item.value"></el-option>
						</el-select>
					</el-form-item>
					<el-form-item label="算费方式标识" required>
						<el-select v-model="formData.waysofcalc" :disabled="disabledGroup.waysofcalc" filterable size="small"  @change="handleCalculateType">
							<el-option v-for="item in calculateTypeOptions" :key="item.value" :label="item.value + '-' + item.label" :value="item.value"></el-option>
						</el-select>
					</el-form-item>
					<el-form-item label="iLog费率配置状态" required>
						<el-select v-model="formData.ilogflag" :disabled="disabledGroup.ilogflag" filterable size="small" >
							<el-option v-for="item in iLogFlagOptions" :key="item.value" :label="item.value + '-' + item.label" :value="item.value"></el-option>
						</el-select>
					</el-form-item>
					<el-form-item label="短期费率方式" required>
						  <el-select v-model="formData.shortratetype" :disabled="disabledGroup.shortratetype" placeholder="请选择" size="small">
										<el-option v-for="item in shortRateOptions" :key="item.shortratetype" :label="item.shortratetype" :value="item.shortratetype"></el-option>
									</el-select>
			  		</el-form-item>
			  		<el-form-item label="条款性质" required>
						  <el-select v-model="formData.kindNature" :disabled="disabledGroup.kindNature" placeholder="请选择" size="small">
										<el-option v-for="item in kindNatureOptions" :key="item.value" :label="item.label" :value="item.value"></el-option>
									</el-select>
			  		</el-form-item>
					<el-form-item label="备注">
						<el-input type="textarea" v-model="formData.remark" :disabled="disabledGroup.remark" :maxlength="4000" size="small" :rows="2"></el-input>
					</el-form-item>
			  	</el-col>
			  	<el-col :span="12">
					<el-form-item label="固定费率(%)">
						<el-input v-model="formData.agreementrate" :disabled="disabledGroup.agreementrate" @keyup.native="checkAgreementRate()" size="small"></el-input>
					</el-form-item>
					<el-form-item label="对应加成条款代码">
						<el-input type="textarea" v-model="formData.pluskindcode" :disabled="disabledGroup.pluskindcode" size="small" readonly @focus="openKindSelectDialog()"></el-input>
					</el-form-item>
					<el-form-item label="加成比例(%)">
						<el-input v-model="formData.plusrate" :disabled="disabledGroup.plusrate" @keyup.native="checkPlusRate()" size="small"></el-input>
					</el-form-item>
					<el-form-item label="条款权限机构代码">
						<el-input type="textarea" v-model="comLabel" :disabled="disabledGroup.powagentcode"  size="small" readonly @dblclick.native="openCompanySelectDialog()"></el-input>
					</el-form-item>
					<el-form-item label="是否配置备案号" required>
						<el-select v-model="formData.efilestatus" :disabled="disabledGroup.efilestatus" filterable size="small"  disabled>
							<el-option v-for="item in eFileOptions" :key="item.value" :label="item.value + '-' + item.label" :value="item.value"></el-option>
						</el-select>
					</el-form-item>
					<el-form-item label="是否累计保额" required>
						<el-select v-model="formData.calculateflag" :disabled="disabledGroup.calculateflag" filterable size="small" >
							<el-option v-for="item in calculateOptions" :key="item.value" :label="item.value + '-' + item.label" :value="item.value"></el-option>
						</el-select>
					</el-form-item>
					<el-form-item label="Cas抄单" required>
						<el-select v-model="formData.cas" :disabled="disabledGroup.calculateflag" filterable size="small" >
							<el-option v-for="item in casOptions" :key="item.value" :label="item.value + '-' + item.label" :value="item.value"></el-option>
						</el-select>
					</el-form-item>
					<el-form-item label="有效标识" required>
						<el-select v-model="formData.validstatus" size="small" >
							<el-option v-for="item in validOptions" :key="item.value" :label="item.label" :value="item.value"></el-option>
						</el-select>
					</el-form-item>
					<el-form-item label="版本生效日期" required>
						<el-date-picker type="date" placeholder="选择日期" v-model="formData.startdate" :disabled="disabledGroup.startdate" style="width: 100%;" :editable="false" size="small"></el-date-picker>
					</el-form-item>
					<el-form-item label="版本失效日期" required>
						<el-date-picker type="date" placeholder="选择日期" v-model="formData.enddate" style="width: 100%;" :editable="false" size="small"></el-date-picker>
					</el-form-item>
			  	</el-col>
			  </el-row>
			</el-form>
			  <span slot="footer" class="dialog-footer">
				   <el-button @click="off">取 消</el-button>
				   <el-button  type="warning" @click="edit">修 改</el-button>
			  </span>
		</el-dialog>

		<KindSelect  v-if="showKindSelectDialog"></KindSelect>
		<ComSelect  v-if="showComSelectDialog"></ComSelect>
	</div>
</template>

<script>

import KindAPI from "../../../../js/production/kind/kindAPI"
import RiskAPI from "../../../../js/production/risk/riskAPI"
import Validations from "../../../../js/production/validations"
import dateUtil from "../../../../common/js/dateUtil";
import KindSelect from "./KindSelect";
import ComSelect from "../../common/ComSelect";
export default {
  	components : {
	  	KindSelect,
	  	ComSelect
	  },
	data() {
		return {
			dialogVisible: true,
			dialog_margin_top: '2%',
			rowdata: {},
			formData: {
				kindcname: '',
				kindename: '',
				ownerriskcode: '',
				kindcode: '',
				kindversion: '',
				kindType: '',
				ilogflag: '',
				calculateflag: '',
				cas: '',
				efilestatus: '',
				waysofcalc: '',
				agreementrate: '',
				pluskindcode: '',
				plusrate: '',
				powagentcode: '',
				validstatus: '',
				remark: '',
				startdate: '',
				enddate: '',
				shortratetype: '',
				kindNature: '',
			},
			comLabel : '',
			arrayComcodes : [],
			riskOptions: [],
			shortRateOptions:[],
			versionOptions: [],
			kinds : [],
			kindTypeOptions: [
				{label: '主险',  value: '1'},
				{label: '附加险 ',  value: '2'},
			],
			kindNatureOptions: [
				{label: '0-物质损失',  value: '0'},
				{label: '1-营业中断',  value: '1'},
			],
			iLogFlagOptions: [
				{label: '是',  value: '1'},
				{label: '否 ',  value: '0'},
			],
			calculateOptions: [
				{label: '是',  value: 'Y'},
				{label: '否 ',  value: 'N'},
			],
			casOptions: [
				{label: '抄明细',  value: 'M'},
				{label: '抄备注 ',  value: 'B'},
			],
			eFileOptions: [
				{label: '是',  value: 'Y'},
				{label: '否 ',  value: 'N'},
			],
			calculateTypeOptions: [
				{label: '手录保费',value: '0'},
				{label: '固定费率',value: '1'},
				{label: 'iLog算费',value: '2'},
				{label: '加成比例',value: '3'},
			],
			orgOptions: [

			],
			validOptions: [
				{label : '1-有效',value: '1'},
				{label : '0-无效',value: '0'}
			],
			disabledGroup: {
				agreementrate : true,
				pluskindcode : true,
				plusrate : true,
				ilogflag : true,
				kindcname: false,
				kindename: false,
				kindversion: false,
				calculateflag: false,
				efilestatus: false,
				waysofcalc: false,
				validstatus: false,
				remark: false,
				startdate: false,
				enddate: false,
				powagentcode: false,
				shortratetype : false,
				kindNature : false,
			},
			showKindSelectDialog: false,
			showComSelectDialog: false,
			loading : false,
			tempNewVersion : '',
		}
	},
	mounted() {
		RiskAPI.initShortRate(this);
		this.rowdata = this.$parent.selectedRows[0];
		let currentVersion = {value : this.rowdata.kindversion};
		let nextVersion = {value : (parseInt(currentVersion.value) + 1) + ".0"};
		this.comLabel = this.rowdata.powagentcode;
		if("undefined" !== typeof(this.rowdata.powagentcode) && this.rowdata.powagentcode !== null && this.rowdata.powagentcode !== '') {
			this.arrayComcodes = this.rowdata.powagentcode.split(',');
		}
		this.versionOptions.push(currentVersion);
		this.versionOptions.push(nextVersion);
		this.formData.kindcname = this.rowdata.kindcname;
		this.formData.kindename = this.rowdata.kindename;
		this.formData.ownerriskcode = this.rowdata.ownerriskcode;
		this.formData.kindcode = this.rowdata.kindcode;
		this.formData.kindversion = this.rowdata.kindversion;
		this.formData.kindType = this.rowdata.kindcode.substring(2,3) == "M" ? "1" : "2";
		this.formData.ilogflag = this.rowdata.ilogflag;
		this.formData.calculateflag = this.rowdata.calculateflag.substring(0,1);
		this.formData.cas = this.rowdata.calculateflag.substring(6);
		this.formData.efilestatus = this.rowdata.efilestatus;
		this.formData.waysofcalc = this.rowdata.waysofcalc;
		this.formData.agreementrate = this.rowdata.agreementrate;
		this.formData.pluskindcode = this.rowdata.pluskindcode;
		this.formData.plusrate = this.rowdata.plusrate;
		this.formData.powagentcode = this.rowdata.powagentcode;
		this.formData.validstatus = this.rowdata.validstatus;
		this.formData.remark = this.rowdata.remark;
		this.formData.startdate = new Date(this.rowdata.startdate);
		this.formData.enddate = new Date(this.rowdata.enddate);
		this.formData.shortratetype = this.rowdata.shortratetype;
		this.formData.kindNature = this.rowdata.calculateflag.substring(5,6);
		this.handleCalculateType();
		if(this.rowdata.validflag == "1" || this.rowdata.validstatus == "1") {
			this.disabledGroup.kindcname= true;
			this.disabledGroup.kindename= true;
			this.disabledGroup.kindversion= false;
			this.disabledGroup.calculateflag= true;
			this.disabledGroup.efilestatus= true;
			this.disabledGroup.waysofcalc= true;
			this.disabledGroup.validstatus= false;
			this.disabledGroup.remark= true;
			this.disabledGroup.startdate= true;
			this.disabledGroup.enddate= false;
			this.disabledGroup.powagentcode= true;
			this.disabledGroup.pluskindcode = true;
			this.disabledGroup.plusrate = true;
			this.disabledGroup.agreementrate = true;
			this.disabledGroup.shortratetype = true;
			this.disabledGroup.ilogflag = true;
			this.disabledGroup.kindNature = true;
		};
	},
	watch : {
		comLabel(newVal,oldVal) {
			this.formData.powagentcode = newVal;
		}
	},
	methods: {
		checkAgreementRate() {
			Validations.checkDecimal(3, 4, this.formData, "agreementrate");
		},
		checkPlusRate() {
			Validations.checkDecimal(3, 4, this.formData, "plusrate");
		},
		edit() {
			if(this.formData.enddate < this.formData.startdate) {
				this.$message({type: 'error',message: '【版本失效日期】不能小于【版本生效日期】'});
				this.formData.enddate = null;
				return;
			}
			if(this.formData.kindversion !== this.$parent.selectedRows[0].kindversion) {
				this.tempNewVersion = this.formData.kindversion;
				this.formData.kindversion = (parseFloat(this.tempNewVersion) - 1.0) + '.0';
			}
			KindAPI.edit(this);
		},
		off() {
			this.$parent.showEditDialog = false;
		},
		handleCalculateType() {
			switch(this.formData.waysofcalc) {
				case '0': {
					this.disabledGroup.agreementrate = true;
					this.disabledGroup.pluskindcode = true;
					this.disabledGroup.plusrate = true;
					this.disabledGroup.ilogflag = true;
					this.formData.agreementrate = '';
					this.formData.pluskindcode = '';
					this.formData.plusrate = '';
					this.formData.ilogflag = '0';
					break;
				}
				case '1': {
					this.disabledGroup.agreementrate = false;
					this.disabledGroup.pluskindcode = true;
					this.disabledGroup.plusrate = true;
					this.disabledGroup.ilogflag = true;
					this.formData.pluskindcode = '';
					this.formData.plusrate = '';
					this.formData.ilogflag = '0';
					break;
				}
				case '2': {
					this.disabledGroup.agreementrate = true;
					this.disabledGroup.pluskindcode = true;
					this.disabledGroup.plusrate = true;
					this.disabledGroup.ilogflag = false;
					this.formData.agreementrate = '';
					this.formData.pluskindcode = '';
					this.formData.plusrate = '';
					break;
				}
				case '3': {
					this.disabledGroup.agreementrate = true;
					this.disabledGroup.pluskindcode = false;
					this.disabledGroup.plusrate = false;
					this.disabledGroup.ilogflag = true;
					this.formData.agreementrate = '';
					this.formData.ilogflag = '0';
				}

			}


		},
		openKindSelectDialog() {
			this.showKindSelectDialog = true;
		},
		openCompanySelectDialog() {
			if(!this.disabledGroup.powagentcode) {
				this.showComSelectDialog = true;
			}
		},
	}
  }
</script>
